<?php
namespace ProjetA\GroupeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

use Symfony\Component\Validator\Constraints\Email as Email;
use Symfony\Component\Validator\Constraints\Date as Date;
use Symfony\Component\Validator\Constraints\Time as Time;
use Symfony\Component\Validator\Constraints\Url as Url;
use Symfony\Component\Validator\Constraints\MaxLength as MaxLength;
use Symfony\Component\Validator\Constraints\Collection as Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ParametreType
 * 
 * @author Gaël Francillette francillette.gael@etu.unice.fr
 */
class ParametreType extends AbstractType{
    //put your code here
    
  

    public function buildForm(FormBuilder $builder, array $options)
    {
      
        // Formulaire auto généré
        $data = $options["data"]->get();
        foreach ($data as $k => $value) {
            $paramchamps = array('property_path' => false);
            if($value["type"] == 'date'){
                $paramchamps = array_merge($paramchamps,array('widget' => 'choice','format' => 'dd-MM-yyyy'));
            }
            if($value['type']=='hidden'){
                $paramchamps = array_merge($paramchamps,array('data' => $value['value']));
            }
            if($value['label']=='groupe_public'){
                $paramchamps = array_merge($paramchamps,$value["param"]); 
            }
            $builder->add($value["label"],$value["type"],$paramchamps);
            
        }  
        
    }
    
     public function getDefaultOptions(array $options)
    {
         
        $collectionConstraint = new Collection(array());
        $cons = array();
                $cons=array_merge(array('groupe_nom'=> new MaxLength(255)),$cons);
                $cons=array_merge(array('groupe_description'=> new MaxLength(255)),$cons);
        $data = $options["data"]->get();      
        foreach ($data as $k => $value){
            switch($value['type']){
                case 'text':
                         $cons=array_merge(array($value['label'] => new MaxLength(255)),$cons);
                break;
            
                case 'email':
                         $cons=array_merge(array($value['label'] => new Email(array('message' => 'adresse email invalide'))),$cons);
                break;
            
                case 'url':
                         $cons=array_merge(array($value['label'] => new Url(array('message' => 'url invalide'))),$cons);
                break;
             
                case 'date':
                         $cons=array_merge(array($value['label'] => new Date(array('message' => 'Date mal renseignée'))),$cons);
                break;
                case 'time':
                         $cons=array_merge(array($value['label'] => new Time(array('message' => 'Heure mal renseignée'))),$cons);
                break;
            
                case 'textarea':
                         $cons=array_merge(array($value['label'] => new MaxLength(255)),$cons);
                break;
            }
                
                
            }
            $collectionConstraint = new Collection($cons);
    

        return array('validation_groups' => array('projeta_groupebundle_parametretype'),
                     'data_class'=>'ProjetA\GroupeBundle\Entity\Parametre',
                     'validation_constraint' => $collectionConstraint);
    }

    public function getName() {
        return 'projeta_groupebundle_parametretype';
    }
}
?>
